package Server;

import Data.*;
import Email.EmailCaptcha;
import Email.ManageCaptcha;
import Email.SendCaptcha;
import MyInterfaces.*;
import Search.*;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.Date;
import java.util.TimerTask;

import Email.*;

public class ChatServer implements Runnable{
    private Socket socket;
    private String userid;

    public ChatServer(Socket socket,String userid) {
        this.socket = socket;
        this.userid=userid;
    }

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    @Override
    public void run() {
        event massage;
        System.out.println("正在连接……");
        while(true)
        {
            try {
                //得到对象流
                ObjectInputStream reader=new ObjectInputStream(socket.getInputStream());
                massage=(event) reader.readObject();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

            if(massage.getType().equals(MassageType.Send_Massage_To_One))
            {
                try {
                    //给一个人发送消息
                    MassageData getmassage=(MassageData) massage.getT();
                    System.out.println("收到了来自"+getmassage.getSender()+"在"+getmassage.getTimestamp()+"的时候"+"给"+getmassage.getReceiver()+"的消息"+getmassage.getContent());
                    //然后需要写入到数据库
                    InsertMassageToSql insertMassageToSql=new InsertMassageToSql(getmassage);
                    boolean k=false;
                    k=insertMassageToSql.insertSql();
                    event returnmassage=new event();
                    if(k)
                    {
                        returnmassage.setType(MassageType.Return_Send_Massage_Success);
                    }
                    else
                    {
                        returnmassage.setType(MassageType.Return_Send_Massage_Fail);
                    }
                    returnmassage.setT(getmassage);
                    ObjectOutputStream writer=new ObjectOutputStream(ManageClientConnectionServerThread.getSocketByUserid(getmassage.getSender()).getOutputStream());
                    writer.writeObject(returnmassage);
                    writer.flush();
                    //写入之后看对方是否在线，发送给他
                    boolean is= ManageClientConnectionServerThread.existId(getmassage.getReceiver());
                    if(is)
                    {
                        //如果这个人在线
                        Socket friendScoket= ManageClientConnectionServerThread.getSocketByUserid(getmassage.getReceiver());
                        System.out.println(getmassage.getReceiver()+"在线");
                        returnmassage.setType(MassageType.Send_Massage_To_One);
                        returnmassage.setT(getmassage);

                        writer=new ObjectOutputStream(friendScoket.getOutputStream());
                        writer.writeObject(returnmassage);
                        writer.flush();
                        System.out.println("转发成功!");
                    }
                    else
                    {
                        //不在线，另外处理
                    }

                } catch (SQLException e) {
                    throw new RuntimeException(e);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            else if(massage.getType().equals(MassageType.Change_Group_Information))
            {
                GroupData groupData=(GroupData) massage.getT();
                ChangeGroupInformation change=new ChangeGroupInformation(groupData);
                try {
                    boolean k=change.change();
                    event returnmassage=new event();
                    if(k)
                    {
                        returnmassage.setType(MassageType.Change_Group_Information_Success);
                        returnmassage.setT(groupData);
                    }
                    else
                    {
                        returnmassage.setType(MassageType.Change_Group_Information_Fail);
                    }
                    //发送过去
                    ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                    writer.writeObject(returnmassage);
                    writer.flush();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            else if(massage.getType().equals(MassageType.Search_Group))
            {
                System.out.println("收到了需要查找好友的消息");
                SearchGroup searchGroup=new SearchGroup((MassageData) massage.getT());
                try {
                    event returnmassage= searchGroup.search();
                    ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                    writer.writeObject(returnmassage);
                    writer.flush();
                    System.out.println("返回请求成功");
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
            else if(massage.getType().equals(MassageType.Add_Group))
            {
                System.out.println("需要加入到数据库当中");
                InsertToGroup res=new InsertToGroup();
                ChatData chatData=(ChatData) massage.getT();
                try {
                    boolean k=res.insert(chatData);
                    event returnmassage=new event();
                    if(k)
                    {
                        returnmassage.setType(MassageType.Add_Group_Success);
                    }
                    else
                    {
                        returnmassage.setType(MassageType.Add_Group_Fail);
                    }
                    ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                    writer.writeObject(returnmassage);
                    writer.flush();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
            else if(massage.getType().equals(MassageType.Request_Init_Information))
            {
                //请求要自己的界面信息
            }
            else if(massage.getType().equals(MassageType.Send_Massage_To_Group))
            {
                //群聊发送消息
                System.out.println("需要群聊转发消息");
                GroupMassageData groupMassageData=(GroupMassageData) massage.getT();
                SendMassageToOther sendMassageToOther=new SendMassageToOther();
                try {
                    boolean is=sendMassageToOther.send(groupMassageData);
                    event returnmassage=new event();
                    if(is)
                    {
                        returnmassage.setType(MassageType.Return_Send_TO_Group_Massage_Success);
                        returnmassage.setT(groupMassageData.getMassageData());
                    }
                    else
                    {
                        returnmassage.setType(MassageType.Return_Send_TO_Group_Massage_Fail);
                    }
                    ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                    writer.writeObject(returnmassage);
                    writer.flush();
                } catch (IOException | SQLException | NoSuchAlgorithmException | ClassNotFoundException e) {
                    throw new RuntimeException(e);
                }

            }
            else if(massage.getType().equals(MassageType.Exit))
            {
                //退出登录
                FriendListData friendListData=(FriendListData)massage.getT();
                ExitLogin exitLogin=new ExitLogin(friendListData);
                try {
                    exitLogin.exit();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

            }
            else if(massage.getType().equals(MassageType.Request_Chat_Massage))
            {
                MassageData massageData=(MassageData) massage.getT();
                MassageListData massageListData=new MassageListData();
                SearchMassage search=new SearchMassage(massageData);
                System.out.println("得到需要发送消息记录的请求");
                try {
                    event returnmassage=new event();
                    massageListData.setMassagelist(search.LoadMassage());
                    massageListData.setSender_id(massageData.getSender());
                    massageListData.setReceiver_id(massageData.getReceiver());

                    returnmassage.setType(MassageType.Return_Chat_Massage);
                    returnmassage.setT(massageListData);
                    ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                    writer.writeObject(returnmassage);
                    writer.flush();
                    //发送消息记录成功
                    System.out.println("发送消息记录成功!");
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
            else if(massage.getType().equals(MassageType.Request_Get_Friend_list_From_Group))
            {
                System.out.println("得到需要好友列表的请求");
                String group_id=(String) massage.getT();
                GroupData groupData=new GroupData();
                SearchFriendFromGroup searchFriendFromGroup=new SearchFriendFromGroup(group_id);
                try {
                    groupData.setGroup_friend_list(searchFriendFromGroup.getFriendListFromGroup());
                    groupData.setGroup_id(group_id);

                    event returnmassage=new event();
                    returnmassage.setType(MassageType.Return_Friend_List_From_Group);
                    returnmassage.setT(groupData);
                    ObjectOutputStream wirter=new ObjectOutputStream(socket.getOutputStream());
                    wirter.writeObject(returnmassage);
                    wirter.flush();
                    System.out.println("好友列表返回成功");
                } catch (ClassNotFoundException | SQLException | IOException e) {
                    throw new RuntimeException(e);
                }
            }
            else if(massage.getType().equals(MassageType.Request_Get_More_Massage_From_Group))
            {
                MassageData massageData=(MassageData) massage.getT();
                MassageListData massageListData=new MassageListData();
                SearchMassageFromGroup search=new SearchMassageFromGroup(massageData);

                event returnmassage=new event();
                try {
                    massageListData=search.searchMassage();

                    returnmassage.setType(MassageType.Return_Chat_Massage_From_Group);
                    returnmassage.setT(massageListData);

                    ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                    writer.writeObject(returnmassage);
                    writer.flush();

                    System.out.println("发送群聊消息成功");
                } catch (ClassNotFoundException | SQLException | IOException e) {
                    throw new RuntimeException(e);
                }
            }
            else if(massage.getType().equals(MassageType.Creat_Group))
            {
                String id=(String) massage.getT();
                CreatGroup creatGroup=new CreatGroup(id);
                try {
                    System.out.println("请求创建一个群聊");
                    String group_id=creatGroup.creatgroup();
                    event returnmassage=new event();
                    if(group_id==null)
                    {
                        //失败了
                        returnmassage.setType(MassageType.Return_Creat_Group_Fail);
                    }
                    else
                    {
                        System.out.println("成功创建");
                        returnmassage.setType(MassageType.Return_Creat_Group_Success);
                        GroupData groupData=new GroupData();
                        groupData.setCreater_id(id);
                        groupData.setGroup_id(group_id);
                        groupData.setId(id);
                        groupData.setIdentity(1);
                        returnmassage.setT(groupData);
                    }
                    ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                    writer.writeObject(returnmassage);
                    writer.flush();

                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
            else if(massage.getType().equals(MassageType.Request_Get_File))
            {
                //请求得到一个文件
                System.out.println("请求得到一个文件");
                SearchFile searchFile=new SearchFile((MassageData) massage.getT());
                System.out.println(((MassageData) massage.getT()).getMD5());
                try {
                    searchFile.search();
                    event returnmassage=new event();
                    if(searchFile.getMassageData().getBytes()!=null)
                    {
                        //说明成功了
                        System.out.println("成功");
                        returnmassage.setType(MassageType.Return_Get_File_Success);
                    }
                    else
                    {
                        System.out.println("失败");
                        returnmassage.setType(MassageType.Return_Get_File_Fail);
                    }
                    returnmassage.setT(searchFile.getMassageData());
                    ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                    writer.writeObject(returnmassage);
                    writer.flush();

                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
            else if(massage.getType().equals(MassageType.Send_File))
            {
                //发送文件给一个人
                System.out.println("收到了需要写入文件的操作");
                MassageData massageData=(MassageData) massage.getT();
                GetFile getFile=new GetFile(massageData);
                try {
                    massageData= getFile.getFile();
                    if(massageData.getMD5()!=null)
                    {
                        event returnmassage=new event();
                        returnmassage.setType(MassageType.Send_File_Success);
                        massageData.setBytes(null);
                        returnmassage.setT(massageData);
                        ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                        writer.writeObject(returnmassage);
                        writer.flush();

                        if(ManageClientConnectionServerThread.existId(massageData.getReceiver()))
                        {
                            //转发文件
                            Socket friendsocket=ManageClientConnectionServerThread.getSocketByUserid(massageData.getReceiver());
                            writer=new ObjectOutputStream(friendsocket.getOutputStream());
                            returnmassage=new event();
                            returnmassage.setType(MassageType.Send_Massage_To_One);
                            returnmassage.setT(massageData);
                            writer.writeObject(returnmassage);
                            writer.flush();
                        }
                    }
                    else
                    {
                        event returnmassage=new event();
                        returnmassage.setType(MassageType.Send_File_Fail);
                        massageData.setBytes(null);
                        returnmassage.setT(massageData);
                        ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                        writer.writeObject(returnmassage);
                        writer.flush();
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
            else if(massage.getType().equals(MassageType.Send_File_To_Group))
            {
                System.out.println("需要向群聊读写文件");
                GroupMassageData groupMassageData=(GroupMassageData) massage.getT();
                SendMassageToOther sendMassageToOther=new SendMassageToOther();
                try {
                    boolean k=sendMassageToOther.send(groupMassageData);
                    event returnmassage=new event();
                    if(k)
                    {
                        //成功了
                        System.out.println("转发成功");
                        returnmassage.setType(MassageType.Return_Send_File_To_Group_Success);
                        returnmassage.setT(groupMassageData.getMassageData());
                    }
                    else
                    {
                        System.out.println("转发失败");
                        returnmassage.setType(MassageType.Return_Send_File_To_Group_Fail);
                        returnmassage.setT(groupMassageData.getMassageData());
                    }
                    ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                    writer.writeObject(returnmassage);
                    writer.flush();
                } catch (IOException | SQLException | NoSuchAlgorithmException | ClassNotFoundException e) {
                    throw new RuntimeException(e);
                }
            }
            else if(massage.getType().equals(MassageType.Get_Captcha))
            {
                //请求发送验证码
                String email=(String) massage.getT();
                //得到验证码
                SendCaptcha sendCaptcha=new SendCaptcha();
                String captcha= null;
                try {
                    captcha = sendCaptcha.getYzm(email);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                EmailCaptcha emailCaptcha=new EmailCaptcha(email,captcha,new Date());
                ManageCaptcha.AddCaptchaByEmail(emailCaptcha);
                System.out.println("得到了验证码");
            }
            else if(massage.getType().equals(MassageType.Search_Friend))
            {
                //请求查找好友
                SearchFriend searchFriend=new SearchFriend((ChatData) massage.getT());
                try {
                    PrivateInformationData mydata=searchFriend.returnFriend();
                    ObjectOutputStream writer;
                    if(mydata.getUserid().equals(""))
                    {
                        //说明没找到
                        event returnmassage=new event();
                        returnmassage.setType(MassageType.Return_Search_Friend_Fail);
                        writer=new ObjectOutputStream(socket.getOutputStream());
                        writer.writeObject(returnmassage);
                        writer.flush();
                    }
                    else
                    {
                        //找到了
                        event returnmassage=new event();
                        returnmassage.setType(MassageType.Return_Search_Friend_Success);
                        returnmassage.setT(mydata);
                        writer=new ObjectOutputStream(socket.getOutputStream());
                        writer.writeObject(returnmassage);
                        writer.flush();
                    }

                } catch (ClassNotFoundException | SQLException | IOException e) {
                    throw new RuntimeException(e);
                }
            }
            else if(massage.getType().equals(MassageType.Add_Friend))
            {
                //如果是要请求加好友
                ChatData chatData=(ChatData) massage.getT();
                AddFriend addFriend=new AddFriend(chatData);
                try {
                    boolean k=addFriend.InsertFriendTosql();
                    if(k)
                    {
                        //添加好友成功
                        event returnmassage=new event();
                        returnmassage.setType(MassageType.Add_Friend_Success);
                        ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                        writer.writeObject(returnmassage);
                        writer.flush();

                        //如果好友在线则刷新好友的列表
                        UserInitStage userInitStage=new UserInitStage(chatData.getReceiver_id());
                        FriendListData friendListData=userInitStage.userInformation();

                        event returnmassage1=new event();
                        System.out.println(chatData.getReceiver_id());
                        Socket first= ManageClientConnectionServerThread.getSocketByUserid(chatData.getReceiver_id());
                        if(first!=null)
                        {
                            returnmassage1.setType(MassageType.Return_Init_Information);
                            returnmassage1.setT(friendListData);
                            writer=new ObjectOutputStream(first.getOutputStream());
                            writer.writeObject(returnmassage1);
                            writer.flush();
                        }
                    }
                    else
                    {
                        event returnmassage=new event();
                        returnmassage.setType(MassageType.Add_Friend_Fail);
                        ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                        writer.writeObject(returnmassage);
                        writer.flush();
                    }
                } catch (ClassNotFoundException | SQLException | IOException e) {
                    throw new RuntimeException(e);
                }
            }
            else if(massage.getType().equals(MassageType.Agree_Add_Friend))
            {
                //同意添加好友
                ChatData chatData=(ChatData) massage.getT();
                AddFriend addFriend=new AddFriend(chatData);
                try {
                    boolean k=addFriend.UpdateFriendTosql();
                    if(k)
                    {
                        //得到发送者的信息，发送给接收者
                        SearchInformationById searchInformationById=new SearchInformationById(chatData.getSender_id());
                        PrivateInformationData data=searchInformationById.getPIDdata();
                        Socket first= ManageClientConnectionServerThread.getSocketByUserid(chatData.getReceiver_id());
                        if(first!=null)
                        {
                            event returnmassage=new event();
                            returnmassage.setType(MassageType.Agree_Add_Friend);
                            data.setIsfriend(1);

                            returnmassage.setT(data);
                            ObjectOutputStream writer=new ObjectOutputStream(first.getOutputStream());
                            writer.writeObject(returnmassage);
                            writer.flush();
                        }

                        //另外的好友也需要得到消息
                        searchInformationById=new SearchInformationById(chatData.getReceiver_id());
                        data=searchInformationById.getPIDdata();
                        Socket second=ManageClientConnectionServerThread.getSocketByUserid(chatData.getSender_id());
                        if(second!=null)
                        {
                            event returnmassage=new event();
                            returnmassage.setType(MassageType.Agree_Add_Friend);
                            data.setIsfriend(1);
                            returnmassage.setT(data);
                            ObjectOutputStream writer=new ObjectOutputStream(second.getOutputStream());
                            writer.writeObject(returnmassage);
                            writer.flush();
                        }
                    }
                    else
                    {
                        //如果不成功，返回一个同意失败的消息,去掉那个请求的列表
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
            else if(massage.getType().equals(MassageType.Refuse_Add_Friend))
            {
                //不同意，删掉该记录
                ChatData chatData=(ChatData) massage.getT();
                AddFriend addFriend=new AddFriend(chatData);
                try {
                    boolean k=addFriend.DeleteFriendTosql();
                    if(k)
                    {
                        //刷新列表
                        //发送者刷新列表
                        event returnmassage=new event();
                        Socket usersocket=ManageClientConnectionServerThread.getSocketByUserid(chatData.getSender_id());
                        returnmassage.setType(MassageType.Delete_Friend);
                        returnmassage.setT(chatData.getReceiver_id());
                        ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                        writer.writeObject(returnmassage);
                        writer.flush();
                    }
                    else
                    {
                        //删除失败，返回一个删除失败的消息
                    }
                } catch (ClassNotFoundException | SQLException | IOException e) {
                    throw new RuntimeException(e);
                }
            }
            else if(massage.getType().equals(MassageType.Delete_Friend)) {
                //删除好友调用函数，并且刷新俩边的值
                ChatData chatData = (ChatData) massage.getT();
                AddFriend addFriend = new AddFriend(chatData);
                try {
                    boolean k = addFriend.DeleteFriendTosql();
                    if (k) {
                        //删除好友
                        event returnmassage1=new event();
                        returnmassage1.setType(MassageType.Delete_Friend);
                        returnmassage1.setT(new String(chatData.getSender_id()));
                        Socket one=ManageClientConnectionServerThread.getSocketByUserid(chatData.getReceiver_id());
                        ObjectOutputStream writer=new ObjectOutputStream(one.getOutputStream());
                        writer.writeObject(returnmassage1);
                        writer.flush();

                        event returnmassage2=new event();
                        returnmassage2.setType(MassageType.Delete_Friend);
                        returnmassage2.setT(new String(chatData.getReceiver_id()));
                        Socket two=ManageClientConnectionServerThread.getSocketByUserid(chatData.getSender_id());
                        if(two!=null)
                        {
                            writer=new ObjectOutputStream(two.getOutputStream());
                            writer.writeObject(returnmassage2);
                            writer.flush();
                        }

                    } else {
                        //如果不成功，返回一个同意失败的消息,去掉那个请求的列表
                    }
                } catch (SQLException | ClassNotFoundException | IOException e) {
                    throw new RuntimeException(e);
                }
            }
            else if(massage.getType().equals(MassageType.Change_Information))
            {
                //请求修改个人信息
                ChangeUserInformation changeUserInformation=new ChangeUserInformation(socket);
                try {
                    System.out.println("开始修改个人信息");
                    changeUserInformation.changeInformation(massage);
                } catch (SQLException | IOException | NoSuchAlgorithmException e) {
                    throw new RuntimeException(e);
                }
            }
            else if(massage.getType().equals(MassageType.Request_Set_Other_Admin))
            {
                //把其他人修改成管理员
                System.out.println("收到了需要修改管理员的消息");
                GroupOperationData groupOperationData=(GroupOperationData) massage.getT();
                GroupOperation groupOperation=new GroupOperation();
                try {
                    groupOperation.changeAdmin(groupOperationData);
                } catch (ClassNotFoundException | SQLException | IOException e) {
                    throw new RuntimeException(e);
                }
            }
            else if(massage.getType().equals(MassageType.Request_Set_Other_Group_Owner))
            {
                System.out.println("收到了需要修改群主的消息");
                GroupOperationData groupOperationData=(GroupOperationData) massage.getT();
                GroupOperation groupOperation=new GroupOperation();
                try {
                    boolean k=groupOperation.changeOwner(groupOperationData);
                    if(!k)
                    {
                        event returnmassage=new event();
                        returnmassage.setType(MassageType.Return_Set_Other_Group_Owner_Fail);

                        ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                        writer.writeObject(returnmassage);
                        writer.flush();
                    }

                } catch (ClassNotFoundException | SQLException | IOException e) {
                    throw new RuntimeException(e);
                }
            }
            else if(massage.getType().equals(MassageType.Request_Get_Rid_Of_Admin))
            {
                //需要去掉管理员
                System.out.println("收到了需要去掉管理员的消息");
                GroupOperationData groupOperationData=(GroupOperationData) massage.getT();
                GroupOperation groupOperation=new GroupOperation();
                try {
                    groupOperation.getRidAdmin(groupOperationData);
                } catch (ClassNotFoundException | SQLException | IOException e) {
                    throw new RuntimeException(e);
                }
            }
            else if(massage.getType().equals(MassageType.Request_Delete_Group))
            {
                GroupData groupData=(GroupData) massage.getT();
                GroupOperation groupOperation=new GroupOperation(groupData);
                try {
                    boolean k=groupOperation.delGroup();
                    event returnmassage=new event();
                    if(k)
                    {
                        returnmassage.setType(MassageType.Return_Delete_Group_Success);
                        returnmassage.setT(groupData);
                        //需要刷新界面
                    }
                    else
                    {
                        returnmassage.setType(MassageType.Return_Delete_Group_Fail);
                    }
                    ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                    writer.writeObject(returnmassage);
                    writer.flush();
                } catch (ClassNotFoundException | SQLException | IOException e) {
                    throw new RuntimeException(e);
                }
            }
            else if(massage.getType().equals(MassageType.Request_Delete_Group_People))
            {
                //删除群友
                GroupOperationData groupOperationData=(GroupOperationData) massage.getT();
                GroupOperation groupOperation=new GroupOperation();
                try {
                    groupOperation.delInGroup(groupOperationData);
                } catch (ClassNotFoundException | SQLException | IOException e) {
                    throw new RuntimeException(e);
                }
            }
            else if(massage.getType().equals(MassageType.Invite_Other_To_Group))
            {
                System.out.println("开始邀请好友啦");
                GroupOperationData groupOperationData=(GroupOperationData) massage.getT();
                GroupOperation groupOperation=new GroupOperation();
                try {
                    groupOperation.addToGroup(groupOperationData);
                } catch (ClassNotFoundException | SQLException | IOException e) {
                    throw new RuntimeException(e);
                }

            }
            else if(massage.getType().equals(MassageType.Agree_Group_Friend))
            {
                System.out.println("收到了同意进群的消息");
                GroupOperationData groupOperationData=(GroupOperationData) massage.getT();
                GroupOperation groupOperation=new GroupOperation();
                try {
                    groupOperation.AgreeJoinInGroup(groupOperationData);
                } catch (ClassNotFoundException | SQLException | IOException e) {
                    e.printStackTrace();
                }
            }
            else if(massage.getType().equals(MassageType.Refuse_Group_Friend))
            {
                //拒绝好友申请，从数据库里面删除
                System.out.println("收到了拒绝进群的消息");
                GroupOperationData groupOperationData=(GroupOperationData) massage.getT();
                GroupOperation groupOperation=new GroupOperation();

                try {
                    groupOperation.RefuseJoinInGroup(groupOperationData);
                } catch (ClassNotFoundException | SQLException | IOException e) {
                    throw new RuntimeException(e);
                }
            }
            else if(massage.getType().equals(MassageType.Request_Get_Friend_Information_From_Group))
            {
                //得到好友的信息
                System.out.println("需要发送好友的个人信息");
                GroupFriendData groupFriendData=(GroupFriendData) massage.getT();
                SearchFriendInformationFromGroup sfif=new SearchFriendInformationFromGroup(groupFriendData);
                try {
                    groupFriendData=sfif.searchInformation();

                    event returnmassage=new event();
                    returnmassage.setType(MassageType.Return_Friend_Information_From_Group);
                    returnmassage.setT(groupFriendData);
                    ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                    writer.writeObject(returnmassage);
                    writer.flush();

                    System.out.println("发送成功");
                } catch (ClassNotFoundException | SQLException | IOException e) {
                    throw new RuntimeException(e);
                }
            }
            else if(massage.getType().equals(MassageType.Add_Common_Sentence))
            {
                System.out.println("收到了需要加入常用语的消息");

                CommonSentenceData commonSentenceData=(CommonSentenceData)massage.getT();
                System.out.println(commonSentenceData.getUserid()+"  "+commonSentenceData.getCommomsentence());
                CommonSentenceOperation kkk=new CommonSentenceOperation();
                try {
                    kkk.add(commonSentenceData);
                } catch (ClassNotFoundException | SQLException | IOException e) {
                    e.printStackTrace();
                }
            }
            else if(massage.getType().equals(MassageType.Delete_Common_Sentence))
            {
                System.out.println("收到了需要删除常用语的消息");
                CommonSentenceData commonSentenceData=(CommonSentenceData)massage.getT();
                System.out.println(commonSentenceData.getUserid()+"  "+commonSentenceData.getCommomsentence());

                CommonSentenceOperation kkk=new CommonSentenceOperation();
                try {
                    kkk.delete(commonSentenceData);
                } catch (ClassNotFoundException | SQLException | IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
